<?php

	require_once('SQL.php');

	# Define: DBKey
	# --------------------------------------
	# Define the key of a db, i.e, how two database related to each other. 
	
	
	# Predefined the relations:
	
	define('ONE_TO_ONE',1);
	define('ONE_TO_MANY',2);
	define('MANY_TO_ONE',3);
	define('MANY_TO_MANY',4);	
	
	
	
	class Joob_Db_Relation {
        
		function __construct($from_class, $to_class, $foreign_key,$from_db="", $to_db="", $relation=ONE_TO_MANY){
			$this->from=$from_class;
			$this->to=$to_class;
			$this->relation=$relation;
			if ($from_db==""){
				$from_obj = new $from_class;
				$this->from_db=$from_obj->getDB();
			}else{
				$this->from_db=$from_db;
			}
			if ($to_db==""){
				$to_obj = new $to_class;
				$this->to_db=$to_obj->getDB();
			}else{
				$this->to_db=$to_db;
			}
			$this->primary_key = $this->from_db.'.id';
			$this->foreign_key = $foreign_key;
			$this->join = " ".$this->primary_key."=".$this->foreign_key." ";
			$this->selector="*";
			$this->limit=0;
			$this->condition="";
		}
		
		
		
		
		# Function: db()
		# --------------------------------------
		# Specify the db and key, in case they are not in the constructor.
		
		public function db($from_db, $to_db, $key=""){
			$this->from_db=$from_db;
			$this->to_db=$to_db;
			if (!Joob_Utils_Word::isEmpty($key)){
				$this->key=$key;
			}
		}
		
        
        # Function: key($key)
        # -------------------------------------------
        # Redefine the key.
        
        public function key($key, $key_value=false){
            $this->key=$key;
            if ($key_value!==false){
                $this->$key=$key_value;
            }
        }
        
        public function appendJoin($operator="and",$foreign_key){
        	$this->join = ' ('.$this->join;
        	$this->join .= " ".$operator." ".$this->primary_key."=".$foreign_key." ";
        	$this->join .= ' )';
        }
		
		# Function: limit()
		# -------------------------------------------
		# Setter of the limit.
		
		public function limit($limit){
			$this->limit=$limit;
		}
		
		
		# Function: order()
		# -------------------------------------------
		# Setting the order type.
		
		public function order($str){
			if ($str==':desc'){
                $this->order=':desc';
            }else{
                $this->order=':asc';
            }
		}
		
        
        
        # Function: condition()
        # -------------------------------------------
        # Setting the condition of the key.
        
        public function condition($cond){
            $this->condition=$cond;
        }
        
		
		# Function: select($selector)
		# -------------------------------------------
		# Setter of the limit.
		
		public function select($selector){
			$this->selector=$selector;
		}
		
		
		# Function: get($key_value, $selector,$limit)
		# -------------------------------------------
		# Get the data specified by the relation.
		# @key: the value of the key.
		# @selector: Define how the data is filter. * means all.
		# @limit: The of the returning data.
	
		public function get(){	
            
			$selector=$this->selector;
			$limit=$this->limit;
			$condition=$this->condition;
			if (!Joob_Utils_Word::isEmpty($condition)){
				$condition=" $condition ";
			}
			
			$class_name=$this->to;
            $order='DESC';
            if ($this->order==':asc'){
                $order='ASC';
            }
            $join = $this->join;
            
			if ($this->relation==ONE_TO_MANY || $this->relation==MANY_TO_MANY){
				if ($limit<=0){
					$limit="";
				}else{
					$limit=" LIMIT $limit";
				}
				$query="SELECT $selector FROM {$this->to_db}, {$this->from_db} WHERE $join  $condition ORDER BY $this->primary_key $order $limit";
//				var_dump($query);die();
				return SQL::to_obj(SQL::get($query),$class_name);
			}elseif ($this->relation==ONE_TO_ONE){
				$query="select $selector from {$this->to_db}, , {$this->from_db} where $join $condition limit 1";
				$result=SQL::single($query);
				if ($result==null){
					return null;
				}
				$obj=new $class_name();
				$obj->extend($result);
				return $obj; # Single object
			}else{
				throw new Exception('Relation unsupported');
			}
		}
		
		public $from, $to, $relation, $from_db, $to_db, $key, $join, $primary_key, $foreign_key;
		
		public $selector, $limit, $order, $condition;
	}
	

?>